package me.firstapp.bbs.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.bbs.base.BaseController;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonAdvertisement;
import me.firstapp.common.module.bbs.JsonMember;
import me.firstapp.common.module.bbs.JsonTopic;
import me.firstapp.common.module.bbs.SearchResult;
import me.firstapp.common.utils.NotificationJson;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.spi.bbs.AdvertisementService;
import me.firstapp.spi.bbs.EmailService;
import me.firstapp.spi.bbs.NotificationService;
import me.firstapp.spi.bbs.SearchService;
import me.firstapp.spi.bbs.TopicService;

@Controller
public class IndexController extends BaseController {

	@Autowired
	private TopicService topicService;

	@Autowired
	private EmailService emailService;

	@Autowired
	private SearchService searchService;

	@Autowired
	private NotificationService notificationService;

	@Autowired
	private AdvertisementService advertisementService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView index(@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, Integer recommend,
			String sortParam, String sortType, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		Integer searchType = 0;
		if (recommend != null && recommend == 1) {
			searchType = 1;
		}
		if ("DESC".equals(sortType)) {
			searchType = 2;
		}
		if ("ASC".equals(sortType)) {
			searchType = 3;
		}
		modelMap.put("searchType", searchType);
		// 话题
		PageObject<JsonTopic> topicPage = topicService.findTopicsPage(pageNo, pageSize, null, null, recommend,
				sortParam, sortType);
		List<Integer> pages = new ArrayList<Integer>();
		for (int i = 1; i <= topicPage.getPage().getTotalPage(); i++) {
			pages.add(i);
		}
		modelMap.put("topicPage", topicPage.getPage());
		modelMap.put("pages", pages);

		// 广告
		ListObject<JsonAdvertisement> adResult = advertisementService.findAvailableAds();
		List<JsonAdvertisement> ads = adResult.getItems();
		modelMap.put("ads", ads);

		modelMap.put("navFlag", 0);
		return new ModelAndView("index/index", modelMap);
	}

	/**
	 * 发送邮箱验证码
	 * 
	 * @param email
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/sendEmailCode.do", method = RequestMethod.POST)
	public void sendEmailCode(String email, HttpServletRequest request, HttpServletResponse response) {
		if (StrUtils.isNULL(email)) {
			JsonMember member = getCurrentLoginMember(request, response);
			email = member.getEmail();
		}
		SingleObject<Object> result = emailService.sendEmailCode(email);
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/getNotification.do", method = RequestMethod.GET)
	public void getNotification(HttpServletRequest request, HttpServletResponse response) {
		// 查询未读通知数量
		Long memberId = getLoginMemberId(request, response);
		if (memberId != null) {
			SingleObject<NotificationJson> result = notificationService.findMemberUnReadNotificationCounts(memberId);
			ResponseUtils.renderJson(response, result);
		}

	}

	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public ModelAndView searchView(String keyword, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		SingleObject<SearchResult> result = searchService.search(keyword);
		modelMap.put("searchResult", result.getObject());
		return new ModelAndView("search/searchResult", modelMap);
	}
}
